package com.webside.seo.controller;
 
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.webside.base.basecontroller.BaseController;
import com.webside.exception.AjaxException;
import com.webside.exception.ServiceException;
import com.webside.exception.SystemException;
import com.webside.series.model.Bdictionary;
import com.webside.series.model.SeriesEntity;
import com.webside.series.service.BdictionaryService;
import com.webside.dtgrid.model.Pager;
import com.webside.dtgrid.util.ExportUtils;
import com.webside.util.Common;
import com.webside.util.PageUtil;
 

@Controller
@Scope("prototype")
@RequestMapping("/seo/")
public class SeoController extends BaseController {

	@Autowired
	private BdictionaryService seoService;
	
	@RequestMapping("listUI.html")
	public String listUI(Model model, Bdictionary dictionary,HttpServletRequest request) {
		try
		{
			PageUtil page = new PageUtil();
			if(request.getParameterMap().containsKey("page")){
				page.setPageNum(Integer.valueOf(request.getParameter("page")));
				page.setPageSize(Integer.valueOf(request.getParameter("rows")));
				page.setOrderByColumn(request.getParameter("sidx"));
				page.setOrderByType(request.getParameter("sord"));
			}
			List<Bdictionary>list=seoService.seoByAll(dictionary);
		    model.addAttribute("list", list);
		    List<Bdictionary>listWord=seoService.seoByKeywords(dictionary);
		    model.addAttribute("listWord", listWord);
		    List<Bdictionary>listIndex=seoService.seoByIndex(dictionary);
		    model.addAttribute("listIndex", listIndex);
		    
		    List<Bdictionary>listTitle=seoService.seoByTitle(dictionary);
		    model.addAttribute("listTitle", listTitle);
		    List<Bdictionary>listResearch=seoService.seoByResearch(dictionary);
		    model.addAttribute("listResearch", listResearch);
		    List<Bdictionary>listMeishu=seoService.seoByMeishu(dictionary);
		    model.addAttribute("listMeishu", listMeishu);
		    List<Bdictionary>listSearch=seoService.seoBySearch(dictionary);
		    model.addAttribute("listSearch", listSearch);
			model.addAttribute("page", page);
			
			return Common.BACKGROUND_PATH + "/seo/form";
		}catch(Exception e)
		{
			throw new SystemException(e);
		}
	}
	
	
	
	@RequestMapping("list.html")
	@ResponseBody
	public Object list(String gridPager,HttpServletResponse response) throws Exception{
		Map<String,Object> parameters = null;
		// 映射Pager对象
		Pager pager = JSON.parseObject(gridPager, Pager.class);
		// 判断是否包含自定义参数
		parameters = pager.getParameters();
		if (parameters.size() < 0) {
			parameters.put("name", null);
		}//3、判断是否是导出操作
		if(pager.getIsExport())
		{
			if(pager.getExportAllData())
			{
				//3.1、导出全部数据
				List<Bdictionary> list = seoService.selectTypeSeo(parameters);
				ExportUtils.exportAll(response, pager, list);
				return null;
			}else
			{
				//3.2、导出当前页数据
				ExportUtils.export(response, pager);
				return null;
			}
		}else
		{
		// 设置分页，page里面包含了分页信息
		Page<Object> page = PageHelper.startPage(pager.getNowPage(),pager.getPageSize());
	
		List<Bdictionary> list = seoService.selectTypeSeo(parameters);
		 
		parameters.clear();
		parameters.put("isSuccess", Boolean.TRUE);
		parameters.put("nowPage", pager.getNowPage());
		parameters.put("pageSize", pager.getPageSize());
		parameters.put("pageCount", page.getPages());
		parameters.put("recordCount", page.getTotal());
		parameters.put("startRecord", page.getStartRow());
		//列表展示数据
		parameters.put("exhibitDatas", list);
		return parameters;
	}
	}
	//添加显示
	@RequestMapping("addUI.html")
	public String addUI(Model model, HttpServletRequest request) {
		Bdictionary channel=new Bdictionary();
		List<Bdictionary> Type_List = seoService.selectTypeByAll(channel);
		model.addAttribute("Type_List", Type_List);
		
		List<Bdictionary>type_List=seoService.selectTypeAll(channel);
		model.addAttribute("type_List", type_List);
		return Common.BACKGROUND_PATH + "/seo/form";
	}
	//添加
	@RequestMapping("add.html")
	@ResponseBody
	public Object add(Bdictionary dictionary)
	{
		Map<String, Object> map = new HashMap<String, Object>();
		
		String channel=dictionary.getpName();
		String englishCode=dictionary.getCode().toUpperCase();
		String code=channel+"_"+englishCode;
		dictionary.setCode(code);
		try
		{
			int count=seoService.insert(dictionary);
			 
			if(count >0 )
			{
				map.put("success", Boolean.TRUE);
				map.put("data", null);
				map.put("message", "添加成功");
			}else
			{
				map.put("success", Boolean.FALSE);
				map.put("data", null);
				map.put("message", "添加失败");
			}
		}catch(ServiceException e)
		{
			throw new AjaxException(e);
		}
		return map;
	}
	
	//修改显示
	@RequestMapping("editUI.html")
	public String editUI(Model model, HttpServletRequest request, Long id) {
		try
		{
			Bdictionary channel=new Bdictionary();
			List<Bdictionary>Type_List=seoService.selectTypeByAll(channel);
			model.addAttribute("Type_List", Type_List);
			Bdictionary dictionary=seoService.findById(id);
			PageUtil page = new PageUtil();
			page.setPageNum(Integer.valueOf(request.getParameter("page")));
			page.setPageSize(Integer.valueOf(request.getParameter("rows")));
			page.setOrderByColumn(request.getParameter("sidx"));
			page.setOrderByType(request.getParameter("sord"));
			model.addAttribute("page", page);
			model.addAttribute("bdictionary", dictionary);

			return Common.BACKGROUND_PATH + "/seo/form";
		}catch(Exception e)
		{
			throw new SystemException(e);
		}
	}
	//修改
		@RequestMapping("edit.html")
		@ResponseBody
		public Object update(String it,String ik,String id,String rt,String rk,String rd,String rs) throws AjaxException
		{
			Map<String, Object> map = new HashMap<String, Object>();
		 
			try
			{   
				Bdictionary dBdictionary=new Bdictionary();
				int tcount=0;
				if(it!=""){
					dBdictionary.setName(it);
					dBdictionary.setCode("TITLE_INDEX");
					int count=seoService.updateSeo(dBdictionary);
					if(count>0){
						tcount=1;
					}
					System.out.println(count);
				}
				if(ik!=""){
					dBdictionary.setName(ik);
					dBdictionary.setCode("KEYWORDS_INDEX");
					int count=seoService.updateSeo(dBdictionary);
					if(count>0){
						tcount=1;
					}
				}
				if(id!=""){
					dBdictionary.setName(id);
					dBdictionary.setCode("DESCRIPTION_INDEX");
					int count=seoService.updateSeo(dBdictionary);
					if(count>0){
						tcount=1;
					}
				}
				if(rt!=""){
					dBdictionary.setName(rt);
					dBdictionary.setCode("TITLE_RESEARCH");
					int count=seoService.updateSeo(dBdictionary);
					if(count>0){
						tcount=1;
					}
				}
				if(rk!=""){
					dBdictionary.setName(rk);
					dBdictionary.setCode("KEYWORDS_RESEARCH");
					int count=seoService.updateSeo(dBdictionary);
					if(count>0){
						tcount=1;
					}
				}
				if(rd!=""){
					dBdictionary.setName(rd);
					dBdictionary.setCode("DESCRIPTION_RESEARCH");
					int count=seoService.updateSeo(dBdictionary);
					if(count>0){
						tcount=1;
					}
				}
				if(rs!=""){
					dBdictionary.setName(rs);
					dBdictionary.setCode("SEARCH_KEYWORDS");
					int count=seoService.updateSeo(dBdictionary);
					if(count>0){
						tcount=1;
					}
				}
			
				if(tcount >0)
				{
					map.put("success", Boolean.TRUE);
					map.put("data", null);
					map.put("message", "编辑成功");
					
				}else
				{
					map.put("success", Boolean.FALSE);
					map.put("data", null);
					map.put("message", "编辑失败");
				}
				
			}catch(Exception e)
			{
				throw new AjaxException(e);
			}
			return map;
		}
			
}
